package week04.homework01MazeGame;

import week04.homework01MazeGame.battle.BattleController;
import week04.homework01MazeGame.map.Maze;
import week04.homework01MazeGame.model.item.Equipment;
import week04.homework01MazeGame.model.role.Monster;
import week04.homework01MazeGame.model.role.Player;
import week04.homework01MazeGame.settings.MonsterGrowth;
import week04.homework01MazeGame.settings.PlayerMove;
import week04.homework01MazeGame.utils.RandomGenerator;

import java.util.Scanner;

/**
 * @author li
 */
public class Game {

    private final Maze maze;

    private final Player player;

    private static final Scanner SCANNER = new Scanner(System.in);

    public Game(int width, int height) {
        maze = new Maze(width, height);
        player = new Player(100, 10, 10, 0, 0, maze);
    }

    public void start() {
        gameLoop();
    }

    private void gameLoop() {
        gameIntroduction();
        System.out.println("P: 游戏开始 Q: 退出游戏");
        System.out.print("请输入相关指令：");
        String command = SCANNER.nextLine();
        switch (command) {
            case "q", "Q" -> {
                System.out.println("游戏结束");
            }
            case "p", "P" -> {
                System.out.println("游戏开始");
                while (true) {
                    maze.display();
                    if (player.getX() == maze.getRow() - 1 && player.getY() == maze.getCol() - 1) {
                        System.out.println("游戏胜利");
                        return;
                    }
                    if (player.getHealth() <= 0) {
                        System.out.println("游戏失败");
                        return;
                    }
                    System.out.println("W:上 S:下 A:左 D:右 M:信息 E:查看背包 U:使用物品 Q:退出");
                    System.out.print("请输入相关指令：");
                    command = SCANNER.nextLine();
                    switch (command) {
                        case "q", "Q" -> {
                            System.out.println("游戏结束");
                            return;
                        }
                        case "w", "W" -> playerMove(PlayerMove.UP);
                        case "s", "S" -> playerMove(PlayerMove.DOWN);
                        case "a", "A" -> playerMove(PlayerMove.LEFT);
                        case "d", "D" -> playerMove(PlayerMove.RIGHT);
                        case "m", "M" -> player.showMessage();
                        case "e", "E" -> player.showInventory();
                        case "u", "U" -> {
                            System.out.println("请输入要使用的物品序号：");
                            player.useItem(SCANNER.nextInt());
                            SCANNER.nextLine();
                        }
                        default -> {
                        }
                    }
                    if (maze.getCell(player.getX(), player.getY()).isMonster()) {
                        MonsterGrowth monsterGrowth = RandomGenerator.randomGenerateMonsterGrowth();
                        boolean victor = BattleController.battle(player, new Monster(monsterGrowth, player.getLevel() + 1));
                        if (player.getHealth() <= 0) {
                            System.out.println("游戏失败");
                            return;
                        } else {
                            if (victor) {
                                System.out.println("恭喜你击败了怪物！");
                                maze.getCell(player.getX(), player.getY()).setMonster(false);
                                if (maze.getCell(player.getX(), player.getY()).isEquipment()) {
                                    System.out.println("恭喜你找到一个装备！");
                                    Equipment equipment = RandomGenerator.randomGenerateEquipment();
                                    System.out.println(equipment.getDescription());
                                    System.out.println("是否要获取？(y/n)");
                                    if ("y".equals(SCANNER.nextLine())) {
                                        player.addItem(equipment);
                                    }
                                } else if (maze.getCell(player.getX(), player.getY()).isPotion()) {
                                    System.out.println("恭喜你找到一个药水！");
                                    System.out.println("是否要获取？(y/n)");
                                    if ("y".equals(SCANNER.nextLine())) {
                                        player.addItem(RandomGenerator.randomGeneratePotion());
                                    }
                                }
                            }
                        }
                    }
                }
            }
            default -> System.out.println("无效的输入，请重新输入！");
        }
    }

    private void playerMove(PlayerMove move) {
        maze.getCell(player.getX(), player.getY()).setPlayerPassed(true);
        maze.getCell(player.getX(), player.getY()).setPlayer(false);
        switch (move) {
            case UP -> player.moveUp();
            case DOWN -> player.moveDown();
            case LEFT -> player.moveLeft();
            case RIGHT -> player.moveRight();
            default -> {}
        }
        maze.getCell(player.getX(), player.getY()).setPlayer(true);
    }

    private void gameIntroduction() {
        System.out.println("===== 迷宫冒险 =====");
        System.out.println("你被困在一个随机生成的迷宫中，需要到达出口才能获胜！");
        System.out.println("【基本操作】");
        System.out.println("- 使用 W/A/S/D 进行移动");
        System.out.println("- M 查看角色状态");
        System.out.println("- E 查看背包物品");
        System.out.println("- U 使用物品");
        System.out.println("- Q 退出游戏");
        System.out.println("【战斗系统】");
        System.out.println("- 遇到怪物会自动进入战斗");
        System.out.println("- 击败怪物可能获得装备/药水");
        System.out.println("- 怪物会随着你的等级变强");
        System.out.println("【物品系统】");
        System.out.println("- 装备可提升属性, 并且装备自行叠加, 不断变强");
        System.out.println("- 药水可恢复生命值\\恢复魔力值\\增强防御力");
        System.out.println("【注意】");
        System.out.println("- 游戏中的指令均不区分大小写");
        System.out.println("====================");
        System.out.println("迷宫游戏开始");
    }

}
